package com.iexbuy.ihg.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <pre>       -------------History------------------
 * DATE        AUTHOR        VERSION        DESCRIPTION
 * 2013-03-14  chenguiban    V01            �½��ĵ�
 * </pre>
 * @author <a href="chenguiban@revenco.com">�¹��</a>
 */
public class DateTools {

	/**
	 * ��õ�ǰʱ�� ���ڸ�ʽΪyyyy-MM-dd 
	 * @return
	 */
	public static String getCurrentDate(){
		DateFormat dateFormat  = new SimpleDateFormat("yyyy-MM-dd");
		String now = dateFormat.format(new Date());
		return now;
	}

	public static String getCurrentTime(){
		DateFormat dateFormat  = new SimpleDateFormat("HH:mm:ss");
		String now = dateFormat.format(new Date());
		return now;
	}
	
	public static String getCurrentDateTime(){
		DateFormat dateFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = dateFormat.format(new Date());
		return now;
	}
	
	public static String getCurrentExactDateTime(){
		DateFormat dateFormat  = new SimpleDateFormat("yyyyMMddHHmmsssss");
		String now = dateFormat.format(new Date());
		return now;
	}
	
	/**
	 * ��õ�ǰʱ���ַ�
	 * @param format ���ڸ�ʽ
	 * @return
	 * @throws Exception
	 */
	public static String getCurrentDate(String format) throws Exception{
		DateFormat dateFormat  = new SimpleDateFormat(format);
		String now = dateFormat.format(new Date());
		return now;
	}
	
	/**
	 * ���ʱ���ʽ������ַ��ʾ
	 * @param date
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static String dateConversString(Date date , String format)throws Exception{
		DateFormat dateFormat  = new SimpleDateFormat(format);
		String newDate = dateFormat.format(date);
		return newDate;
	}
	
	
	/**
	 * ����date��ss���Ľṹ
	 * @param date
	 * @param ss
	 * @return
	 */
	public static Date plusSecond(Date date, int ss){
		try{
			Calendar now = Calendar.getInstance();
			now.setTime(date);
		    now.add(Calendar.SECOND, +(ss)); 
			return now.getTime();
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getDateWithPlusDay(Date date, int datePlus) {
		try{
			Calendar now = Calendar.getInstance();
			now.setTime(date);
		    now.add(Calendar.DATE, datePlus); 
			DateFormat dateFormat  = new SimpleDateFormat("yyyy-MM-dd");
			return dateFormat.format(now.getTime());
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 日期加减
	 *
	 * @param d
	 * @param field
	 *            类型
	 * @param amount
	 *            数量
	 * @return
	 */
	public static Date add(Date d, int field, int amount) {
		if (d == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(d);
		calendar.add(field, amount);
		return calendar.getTime();
	}
	
	/**
	 * 取得date的时间
	 * 
	 * @return
	 */
	public static Date getDate(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * 将日期转换成字符窜
	 * 
	 * @return
	 */
	public static String getDateToString(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}
	
	/**
	 * 添加天
	 *
	 * @param d
	 * @param amount
	 * @return
	 */
	public static Date addDay(Date d,  int amount) {
		return add(d, Calendar.DATE, amount);
	}
	
	/**
	 * 指定的日期加天数
	 * @param date
	 * @param amount
	 * @return
	 */
	public static String addDateToString(String date,int amount){
		if (date == null || "".equals(date.trim())) {
			return "";
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getDate(date));
		calendar.add(Calendar.DATE, amount);
		return getDateToString(calendar.getTime());
	}
	
	/**
	 * 取得date的时间
	 * 
	 * @return
	 */
	public static Date getDateTime(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}
	
	/** * 获取指定日期是星期几
	  * 参数为null时表示获取当前日期是星期几
	  * @param date
	  * @return
	*/
	public static String getWeekOfDate(Date date) {      
	    String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};        
	    Calendar calendar = Calendar.getInstance();      
	    if(date != null){        
	         calendar.setTime(date);      
	    }        
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0){        
	        w = 0;      
	    }      
	    return weekOfDays[w];    
	}
	
	/**
     * 得到两个日期之间的日期包括两个日期.
     * @param p_start   Start Date
     * @param p_end     End Date
     * @return Dates List
     */
    public static List<String> getDates(String p_start, String p_end) {
    	Date date =DateTools.getDate(p_start);
    	Calendar startcalendar = Calendar.getInstance();
    	startcalendar.setTime(date);
    	Date date2 =DateTools.getDate(p_end);
    	Calendar endcalendar = Calendar.getInstance();
    	endcalendar.setTime(date2);
        List<String> result = new ArrayList<String>();
        while (startcalendar.before(endcalendar)) {
            result.add(DateTools.getDateToString(startcalendar.getTime()));
            startcalendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        result.add(p_end);
        return result;
    }
    
    public static String getFistDayofMonth(){
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
    	//获取当前月第一天
        Calendar c = Calendar.getInstance();    
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String first = format.format(c.getTime());
        return first;
    }
    
    public static String getLastDayofMonth(){
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
    	 //获取当前月最后一天
    	 Calendar ca = Calendar.getInstance();    
	    ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
	    String last = format.format(ca.getTime());
        return last;
    }
    
   
    //获取某年某月的最后一天
    public static String getLastDayofMonth(int year, int month){
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
    	Calendar ca = Calendar.getInstance();
    	ca.set(year, month-1, 1);
	    ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
	    String last = format.format(ca.getTime());
        return last;
    }
    
    /**
     * 返回指定日期的月的第一天
     *
     * @return
     */
    public static Date getFirstDayOfMonth(String date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDate(date));
        calendar.set(calendar.get(Calendar.YEAR),
                     calendar.get(Calendar.MONTH), 1);
        return calendar.getTime();
    }

   
    /**
     * 返回指定日期的月的最后一天
     *
     * @return
     */
    public static Date getLastDayOfMonth(String date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDate(date));
        calendar.set(calendar.get(Calendar.YEAR),
                     calendar.get(Calendar.MONTH), 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }
    
	
}
